package cn.dglydrpy.study.algorithm.od;

import java.util.*;

/**
 * @author Yyy
 * @date 2024/11/7 22:40
 * @description
 * 石头剪刀布游戏，用字母 A、B、C 分别表示石头、刀、布，根据玩家的出拳情况判断赢家，
 * 若没有优胜的出拳形状则为平局，输出赢家的玩家 ID 列表或 “NULL”。
 * 
 输入：
 abc1 A
 def A
 alic A
 xyz B
 
 输出：
abc1 
def 
alic
 */
public class Od0024_Caiquan {
    public static void main(String[] args) {

        ArrayList<String[]> list = new ArrayList<>();
        
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {

            String input = scanner.nextLine();
            if(input.isEmpty()){
                break;
            }
            String[] s = input.split(" ");
            list.add(s);
        }
            
        String ans = myAnswer(list);
        System.out.println(ans);

        String answer1 = answer1(list);
        System.out.println(answer1);
     
    }

    private static String answer1(ArrayList<String[]> list) {
        List<String> aList = new ArrayList<>();
        List<String> bList = new ArrayList<>();
        List<String> cList = new ArrayList<>();
        for (String[] item : list) {
            if ("A".equals(item[1])) {
                aList.add(item[0]);
            } else if ("B".equals(item[1])) {
                bList.add(item[0]);
            } else if ("C".equals(item[1])) {
                cList.add(item[0]);
            }
        }
        boolean hasA =!aList.isEmpty();
        boolean hasB =!bList.isEmpty();
        boolean hasC =!cList.isEmpty();
        if (hasA && hasB &&!hasC) {
            return output(aList);
        } else if (hasB && hasC &&!hasA) {
            return output(bList);
        } else if (hasA && hasC &&!hasB) {
            return output(cList);
        } 
        return "NULL";
    }
    private static String output(List<String> list) {
        Collections.sort(list);
        
        StringBuilder ans = new StringBuilder();
        list.forEach((o) -> ans.append(o).append(" "));
        return ans.toString().trim();
    }

    private static String myAnswer(ArrayList<String[]> list) {
        StringBuilder ans = new StringBuilder();
        
        ArrayList<String> aList = new ArrayList<>();
        ArrayList<String> bList = new ArrayList<>();
        ArrayList<String> cList = new ArrayList<>();
        for(String[] arr : list){
            if("A".equals(arr[1])){
                aList.add(arr[0]);
            }
            if("B".equals(arr[1])){
                bList.add(arr[0]);
            }
            if("C".equals(arr[1])){
                cList.add(arr[0]);
            }
        }
        
        if(cList.isEmpty() && !aList.isEmpty() && !bList.isEmpty()){
            Collections.sort(aList);
            for(String a : aList){
                ans.append(a).append(" ");
            }
        }
        else if(bList.isEmpty() && !aList.isEmpty() && !cList.isEmpty()){
            Collections.sort(cList);
            for(String a : cList){
                ans.append(a).append(" ");
            }
        }
        else if(aList.isEmpty() && !bList.isEmpty() && !cList.isEmpty()){
            Collections.sort(bList);
            for(String a : bList){
                ans.append(a).append(" ");
            }
        }else{
            ans.append("NULL");
        }
        return ans.toString().trim();
    }
}
